﻿using P002_SudukuSolver_Mvvm.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace P002_SudukuSolver_Mvvm.Commands
{
    public class StopCommand : ICommand
    {
        public event EventHandler? CanExecuteChanged;

        public bool CanExecute(object? parameter)
        {
            //if (MainWindowViewModel.Instance != null)
            //{
            //    // 因为这个命令能不能使用是由SolveState决定的，所以当这个属性改变时必须通知这个命令
            //    // 也就是调用命令的 CanExecuteChanged 事件，这样关注了这个命令的按钮就会得到通知
            //    SolveState state = MainWindowViewModel.Instance.SolveState;
            //    return state == SolveState.Solving || state == SolveState.Pause;
            //}
            //return false;
            return true;
        }

        public void Execute(object? parameter)
        {
            if (MainWindowViewModel.Instance != null)
            {
                var Main = MainWindowViewModel.Instance;

                Main.SolveReset.Set(); // 必须先继续这个线程，才能让线程执行到取消标记点
                Main.SolveCancel.Cancel();
                //while (true)
                //{
                //    if (Main.SolveTask.IsCompleted) break; // 循环等待上一次任务结束
                //}

                Main.SolveState = SolveState.Stop;
            }
        }
    }
}
